﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using BF.IBLL.BaseSystem;
using Base.Core.UnityFactory;
using BF.Common;

namespace BF.Web.pages.BaseSystem.UserManagement
{
    public partial class UserModifyRoles : BasePage
    {

        Isys_UserInfoBLL iBLL = IOCFactory.GetIOCResolve<Isys_UserInfoBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRoles();
            }
        }

        #region 绑定角色用户控件
        protected void BindRoles()
        {
            string[] list = null;

            list = Roles.GetRolesForUser(GetSessionCurrentUserInfo.sLoginName);

            if (GetSessionCurrentUserInfo.sLoginName.ToLower() == "admin")
            {
                list = Roles.GetAllRoles();
            }
            foreach (string val in list)
            {
                string subVal = val.Length > 6 ? val.Substring(0, 6) + "…" : val;
                ListItem item = new ListItem(subVal);
                item.Value = val;
                item.Attributes.Add("style", "display:block;width:117px;float:left;");
                item.Attributes.Add("onmouseover", "ShowToolTip(event,'" + val + "')");
                UserRoles.Items.Add(item);
            }
        }
        #endregion

        //获取要绑定角色的用户Id集合
        protected List<string> UserIdList
        {
            get
            {
                string jsonString = hidUserIdArray.Value;
                if (jsonString.Trim() == "")
                    return new List<string>();
                return JsonHelper<string>.JsonStringToList(jsonString);
            }
        }

        private void GetRoles(List<String> roles)
        {
            roles.Clear();
            roles.AddRange(from ListItem rolebox in UserRoles.Items where rolebox.Selected select rolebox.Value);
        }

        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            List<string> sUserIdList = UserIdList;
            List<Guid> UserList = new List<Guid>();
            foreach (string sUserId in sUserIdList)
            {
                Guid gUserId = Guid.Parse(sUserId);
                UserList.Add(gUserId);
            }
            List<String> roles = new List<string>();
            GetRoles(roles);
            if (iBLL.ModifyUserRoles(UserList, roles))
            {
                Messages("保存成功！", "function(e){winer.RefurbishParent();}", this.Updatepanel1);
            }
            else
            {
                Messages("保存失败！");
            }
        }
    }
}